﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class ArticleManager : System.Web.UI.UserControl
    {
        BaiViet tac = new BaiViet();
        Article art;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            //if(!Page.IsPostBack){
                lbxMenu.DataSource = LinqDataSource6;
                lbxMenu.DataBind();
            //}
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
          
            art = new Article();
            art.ID = Convert.ToInt32(GridView1.DataKeys[GridView1.SelectedIndex].Value);
            art.MenuID = 0;
            if (lbxMenu.SelectedValue != "")
            {
                art.MenuID = Convert.ToInt32(lbxMenu.SelectedValue);
            }
            
            art.Title = TxtTitle.Text;
            art.ThumbImage = TxtImage.Text;
            art.Description = Txtdes.Text;
            art.Content = CKEditor1.Text;
            art.CreateDate = DateTime.Now;

            if (CbxSlide.Checked == false)
            {
                art.IsSilde = false;
            }
            else
            {
                art.IsSilde = true;
            }

            if (CbxFront.Checked == false)
            {
                art.IsFrontPage = false;
            }
            else
            {
                art.IsFrontPage = true;
            }

            art.AuthorID = 0;
            if (LbxAut.SelectedValue != "")
            {
                art.AuthorID = Convert.ToInt32(LbxAut.SelectedValue);
            }
            art.MetaTitle = TxtMetatit.Text;
            art.MetaDescription = TxtMetades.Text;
            art.MetaKeywords = TxtMetakey.Text;
            art.MetaRobots = TxtMetaro.Text;
            art.MetaAuthor = TxtMetaAut.Text;

            Lblchk.Text = tac.Updatearticle(art);
            Response.Redirect("QuanlyBaiViet.aspx");
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
                Panel1.Visible = true;
                string ID =GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
                TxtTitle.Text =HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[4].Text);
                Txtdes.Text = ((Label)GridView1.SelectedRow.FindControl("Lbldes")).Text;

                if (Convert.ToInt32(((Label)GridView1.SelectedRow.FindControl("LblMenuID")).Text) > 0)
                {
                    
                    lbxMenu.SelectedValue = ((Label)GridView1.SelectedRow.FindControl("LblMenuID")).Text;
                        
                }
                TxtImage.Text = ((Label)GridView1.SelectedRow.FindControl("LblThumbi")).Text;
                CKEditor1.Text = ((Label)GridView1.SelectedRow.FindControl("Lblconte")).Text;
                if (((CheckBox)GridView1.SelectedRow.Cells[6].Controls[0]).Checked)
                { CbxSlide.Checked = true; }
                else { CbxSlide.Checked = false; }
                if (((CheckBox)GridView1.SelectedRow.Cells[7].Controls[0]).Checked)
                { CbxFront.Checked = true; }
                else { CbxFront.Checked = false; }

                if (Convert.ToInt32(((Label)GridView1.SelectedRow.FindControl("Lblauthor")).Text) > 0)
                {
                    LbxAut.SelectedValue = ((Label)GridView1.SelectedRow.FindControl("Lblauthor")).Text;
                }
                TxtMetatit.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[8].Text);
                TxtMetades.Text = ((Label)GridView1.SelectedRow.FindControl("LblMedes")).Text;
                TxtMetakey.Text = ((Label)GridView1.SelectedRow.FindControl("LblMekey")).Text;
                TxtMetaro.Text = ((Label)GridView1.SelectedRow.FindControl("Lblmero")).Text;
                TxtMetaAut.Text = ((Label)GridView1.SelectedRow.FindControl("Lblmetaau")).Text;
           
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbx");
                if (cb != null && cb.Checked)
                {
                    tac.DeleteArticle(Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value));
                }
            }
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Txtdes.Text = string.Empty;
            TxtImage.Text = string.Empty;
            TxtMetaAut.Text = string.Empty;
            TxtMetades.Text = string.Empty;
            TxtMetakey.Text = string.Empty;
            TxtMetaro.Text = string.Empty;
            TxtMetatit.Text = string.Empty;
            TxtTitle.Text = string.Empty;
            CbxFront.Checked = false;
            CbxSlide.Checked = false;
            CKEditor1.Text = string.Empty;   
        }

        protected void CbxDele_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbx");
                if (CbxDele.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemBaiViet.aspx");
           
        }

        protected void Ddlposi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Ddlposi.SelectedValue == "Chủ Đề")
            {
                lbxMenu.DataSource = LinqDataSource5;
                lbxMenu.DataBind();
            }
            else
            {
                lbxMenu.DataSource = LinqDataSource1;
                lbxMenu.DataBind();
            }
        }
    
    }
}